package org.zhu.table2word;

import org.zhu.table2word.bean.FieldInfo;
import org.zhu.table2word.bean.MysqlConfig;
import org.zhu.table2word.bean.TableField;

import java.sql.*;
import java.util.*;
import java.util.stream.IntStream;

public class Client {
    public static String tableHeader = "字段名\t说明\t类型\t是否为空\t主键";
    public static void main(String[] args) {
        MysqlConfig mysqlConfig =getMysqlConfig();
        List<TableField> tableFieldList = getMysqlTables(mysqlConfig);
        table2csv(tableFieldList);
    }

    public static void table2csv(List<TableField> tableFieldList){
        tableFieldList.forEach( tableField->{
            System.out.println(tableField.tableName);
            System.out.println(tableHeader);
            tableField.fieldInfoList.forEach(fieldInfo -> {
                System.out.println(fieldInfo.getName()+"\t"+""+"\t"+fieldInfo.getType()+"\t"+fieldInfo.getIsNull()+"\t"+
                        (fieldInfo.getKey().equals("PRI")?"主键":""));
            });                }
        );


    }
    private static MysqlConfig getMysqlConfig(){

        return new MysqlConfig("jdbc:mysql://localhost:3306/biye?useUnicode=true&characterEncoding=UTF-8",
                "root","olive888","com.mysql.jdbc.Driver");
    }

    private static  List<TableField> getMysqlTables(MysqlConfig mysqlConfig){
       List<TableField> tableFieldList = new ArrayList<>();

        Connection conn = null;
        PreparedStatement pstmt = null;

        ResultSetMetaData rsmd = null;
        try {
            conn =  getConnections(mysqlConfig);
            pstmt = conn.prepareStatement("SHOW TABLES");
            ResultSet tableResultSet = pstmt.executeQuery();
            while (tableResultSet.next()){
                TableField tableField = new TableField();
                tableField.tableName = tableResultSet.getString(1);
                //获取表名后 继续获取其字段
                pstmt = conn.prepareStatement("SHOW COLUMNS FROM "+tableResultSet.getString(1));
                ResultSet fieldResultSet = pstmt.executeQuery();

                while (fieldResultSet.next()){
                    FieldInfo fieldInfo = new FieldInfo(
                            fieldResultSet.getString("Field"),
                            fieldResultSet.getString("Type"),
                            fieldResultSet.getString("Null"),
                            fieldResultSet.getString("Key"),
                            fieldResultSet.getString("Default"),
                            fieldResultSet.getString("Extra")
                    );
                    tableField.fieldInfoList.add(fieldInfo);
                }
                tableFieldList.add(tableField);

            }
        } catch (Exception e) {
        e.printStackTrace();
    }finally{
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return tableFieldList;
    }

    //获取连接
    private static Connection getConnections(MysqlConfig mysqlConfig) throws Exception {
        Connection conn = null;
        try {
            Properties props = new Properties();
            props.put("remarksReporting", "true");
            props.put("user", mysqlConfig.getUsername());
            props.put("password", mysqlConfig.getPassword());
            Class.forName(mysqlConfig.getDriver());
            conn = DriverManager.getConnection(mysqlConfig.getUrl(), props);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return conn;
    }
}
